ARG PYTHON_VERSION=3.10.14

FROM python:${PYTHON_VERSION}-slim AS python_env

ARG PYTHON_NAME=python310
ARG BUILD_VENV=/dist
ARG ACTIVATE_VENV=". ${BUILD_VENV}/bin/activate"
ARG EXTRA_LIBS=""
# 华为源
ARG APT_SOURCE=repo.huaweicloud.com

ENV PIP_INDEX_URL="https://mirrors.aliyun.com/pypi/simple/"
ENV TORCH_COMMAND="pip install torch==2.1.2 torchvision==0.16.2 --extra-index-url https://mirrors.aliyun.com/pytorch-wheels/cu118/"

COPY *.whl /wheels/

# 替换国内源
RUN sed -i "s@deb.debian.org@${APT_SOURCE}@g" /etc/apt/sources.list.d/debian.sources \
    && apt-get update

RUN python -m venv ${BUILD_VENV} && chmod +x ${ACTIVATE_VENV}

# installs
RUN ${ACTIVATE_VENV} \
    && [ -z "$(ls -A /wheels)" ] || (cd /wheels && python -m pip install $(ls /wheels -1 | tr '\n' ' ')) \
    && [ -z "${EXTRA_LIBS}" ] || python -m pip install ${EXTRA_LIBS} \
    && python -m ${TORCH_COMMAND} \
    && tar -zcf /venv.tar.gz ${BUILD_VENV}

FROM alpine:edge

COPY --from=python_env /venv.tar.gz /venv.tar.gz

CMD ["tar", "-zxvf", "/venv.tar.gz", "-C", "/venv"]
